package cn.signalpha.modules.base.controller.wx;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishItem;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/wx/material")
@Api(value = "微信素材库", tags = "微信素材库")
public class WxMaterialController {

    private final WxMpService wxMpService;

	@GetMapping("/count")
	@ApiOperation(value = "文件素材总数", notes = "")
	public R<WxMpMaterialCountResult> count() throws WxErrorException {
		return R.data(wxMpService.getMaterialService().materialCount());
	}

	@GetMapping("/page")
	@ApiOperation(value = "文件素材分页", notes = "type：image、video、voice")
	public R<IPage<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem>> page(@RequestParam String type, Query query) throws WxErrorException {
		int offset = (query.getCurrent() - 1) * query.getSize();
		WxMpMaterialFileBatchGetResult wxMpMaterialFileBatchGetResult = wxMpService.getMaterialService().materialFileBatchGet(type, offset, query.getSize());
		IPage<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> pageVo = new Page(query.getCurrent(), query.getSize(), wxMpMaterialFileBatchGetResult.getTotalCount());
		pageVo.setRecords(wxMpMaterialFileBatchGetResult.getItems());
		return R.data(pageVo);
	}

	@GetMapping("/free/publish/page")
	@ApiOperation(value = "已发布的文章分页", notes = "")
	public R<IPage<WxMpFreePublishItem>> freePublishPage(Query query) throws WxErrorException {
		int offset = (query.getCurrent() - 1) * query.getSize();
		WxMpFreePublishList wxMpFreePublishList = wxMpService.getFreePublishService().getPublicationRecords(offset, query.getSize(), 1);
		IPage<WxMpFreePublishItem> pageVo = new Page(query.getCurrent(), query.getSize(), wxMpFreePublishList.getTotalCount());
		pageVo.setRecords(wxMpFreePublishList.getItems());
		return R.data(pageVo);
	}

	@SneakyThrows
	@PostMapping("/put/file")
	@ApiOperation(value = "上传永久文件素材", notes = "mediaType：image、video、voice")
	public R<WxMpMaterialUploadResult> putFile(@RequestParam MultipartFile multipartFile, @RequestParam(required = false) String mediaType) {
		String fileName = multipartFile.getOriginalFilename();
		File file = new File(Objects.requireNonNull(fileName));
		FileUtils.writeByteArrayToFile(file, multipartFile.getBytes());
		WxMpMaterial wxMpMaterial = new WxMpMaterial();
		wxMpMaterial.setName(multipartFile.getOriginalFilename());
		wxMpMaterial.setFile(file);
		wxMpMaterial.setVideoTitle(multipartFile.getOriginalFilename());
		wxMpMaterial.setVideoIntroduction("INTRODUCTION");
		WxMpMaterialUploadResult wxMpMaterialUploadResult = wxMpService.getMaterialService().materialFileUpload(mediaType, wxMpMaterial);
		return R.data(wxMpMaterialUploadResult);
	}
}
